package main;

import common.log.WindLog;

import dataset.DataSetInterface;

public class RefreshThread extends Thread {

	private DataSetInterface dsi;
	private int sleepmillisec;
	public static final int DEFAULT_REFRESHTIME = 1800000;
	public static final int MIN_REFRESHTIME = 600000;
	private WindTrayIconNotification iconNot;

	public void setIconNot(WindTrayIconNotification iconNot) {
		this.iconNot = iconNot;
	}

	public RefreshThread(DataSetInterface aDataSet, int sleepMillisec) {
		super("RefreshThread");
		dsi = aDataSet;
		init(sleepMillisec);
	}

	private void init(int sleepMillisec){
		if(sleepMillisec>=MIN_REFRESHTIME)
			sleepmillisec = sleepMillisec;
		else
			sleepmillisec = MIN_REFRESHTIME;
		
		this.setDaemon(true);
        this.setPriority(Thread.MIN_PRIORITY);
	}
	
	private void notifyIcon(int aIcon){
		if(iconNot!=null)
			iconNot.changeIcon(aIcon);
	}
	
	public void run() {
		while (true) {
			try {
				sleep(sleepmillisec);
				notifyIcon(WindTrayIcon.ICON_REFRESH);
				dsi.refreshWindData();
				notifyIcon(WindTrayIcon.ICON_DEFAULT);
			} catch (Exception e) {
				notifyIcon(WindTrayIcon.ICON_ERROR);
				WindLog.getLogger().logSevere("Running " + this.getName() + ": " + e.getMessage());
			}
		}
	}

}
